package com.lcf.system.service;

import cn.hutool.core.util.RandomUtil;
import com.lcf.redis.enums.RedisCachePrefixEnum;
import com.lcf.redis.utils.RedisCacheUtils;
import com.lcf.sms.*;
import com.lcf.system.dto.sms.SmsDTO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.Collections;
import java.util.List;

/**
 * 短信
 *
 * @author lcf
 */
@Service
public class SmsService {
    @Resource
    private RedisCacheUtils redisCacheUtils;
    @Resource
    private SmsConfig smsConfig;
    @Resource
    private SmsTemplateConfig smsTemplateConfig;

    public void sendBindPhoneCode(SmsDTO phone) {
        String code = RandomUtil.randomNumbers(6);

        List<String> templateParam = Collections.singletonList(code);
        List<String> phoneList = Collections.singletonList(phone.getPhone());

        SmsUtils.send(new SmsReq(smsConfig, smsTemplateConfig.getBindPhone(), templateParam, phoneList));

        String key = RedisCachePrefixEnum.PHONE_BIND_CODE.getKey(code);
        redisCacheUtils.cache(key, phone.getPhone(), Duration.ofMinutes(5));
    }
}
